package panel.viewSpots
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ResourceLoader;
	import assists.resource.ResourceLoaderData;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import inerface.IViewSpotDetailPanel;
	
	import main.ClientConstants;
	
	import panel.viewSpots.navigationNew.PicSlideGroup;
	import panel.viewSpots.navigationNew.ViewSpotDetailItem;
	
	
	public class ViewSpotDetailNew extends Sprite implements IViewSpotDetailPanel
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var _nameTxt:TextField = new TextField();
		private var _picGroup:PicSlideGroup;
		private var _detailContainer:Sprite = new Sprite();
		private static var ROW_DIS:int;
		private static var ICON_POSX:int;
		private static var ICON_TXT_DIS:int;
		
		private var _descContainer:Sprite = new Sprite();
		private var _openTextPanel:Function;
		private var _itemList:Array = [];
		private var _openCommentPanel:Function;
		private var _openDetailPanel:Function;
		
		private var _aroundGroup:AroundGroup;
		private var _dataList:Array;
		
		private var _btnsGroup:ViewSpotDetailBtnGroup;
		private var _type:uint;
		private var _openMap:Function;
		private var _onOpenWeibo:Function;
		private var _addPath:Function;
		private var _commentItem:ViewSpotDetailItem;
		private var _container:Sprite = new Sprite();
		
		private var onShowLocation:Function;
		
		public function ViewSpotDetailNew()
		{
		}
		
		public function update(data:Object):void
		{
			var info:Object = {};
			for(var i:int=0;i<data.propertyList.length;i++)
			{
				info = data.propertyList[i];
				if(info.type == "commentList")
				{
					break;
				}
			}
			_commentItem.updateText({desc:info.value,type:info.type,value:info.value2});
		}
		
		public function set onPanelTyep(value:uint):void
		{
			_type = value;
		}
		
		public function init(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			ROW_DIS = _stage.stageHeight * 0.02;
			var txtWid:int = _stage.stageWidth * 0.9;
			
			_nameTxt.mouseEnabled = false;
			_nameTxt.autoSize = "left";
			_nameTxt.width = txtWid;
			_nameTxt.wordWrap = true;
			_nameTxt.multiline = true;
			
			_picGroup = new PicSlideGroup();
			_picGroup.init(_stage,_loader);
			_btnsGroup = new ViewSpotDetailBtnGroup(_loader,_stage);
		}
		
		public function show(data:Object,dataList:Array):void
		{
			if(dataList != null) _dataList = dataList;
			
			_container.y = _stage.stageHeight * ClientConstants.HOME_UP_PER;
			_container.addChild(_btnsGroup);
			this.addChild(_container);
			TextFieldUtils.setHtmlText(_nameTxt,HtmlText.format(data.name,0xffffff,24,true));
			
			_btnsGroup.onItemInfo(_type,data);
			_btnsGroup.onAddPath = _addPath;
			_btnsGroup.onOpenWeibo = _onOpenWeibo;
			
			var picList:Array = [];
			var preStr:String = "";
			if(_type == ClientConstants.DETAIL_HOUSE)
			{
				preStr = "hotel/";
			}else if(_type == ClientConstants.DETAIL_VIEWSPOT)
			{
				preStr = "viewSpot/";
			}else if(_type == ClientConstants.DETAIL_SHOP)
			{
				preStr = "scenicShop/";
			}else if(_type == ClientConstants.DETAIL_EAT)
			{
				preStr = "eat/";
			}
			for(var i:int=0;i<data.picList.length;i++)
			{
				var bmp:Bitmap = _loader.getImage(data.picUrl[i]);
				if(bmp == null) continue;
				bmp.width = _stage.stageWidth;
				bmp.height = _stage.stageHeight * 0.3;
				picList.push(bmp);
			}
			_picGroup.show(picList);
			layout();
			var obj:Object;
			for(i = 0;i < data.propertyList.length;i++)
			{
				obj = data.propertyList[i];
				var item:ViewSpotDetailItem = new ViewSpotDetailItem(_stage,_loader);
				item.openTextPanel = _openTextPanel;
				item.openCommitPanel = _openCommentPanel;
				item.openMap = _openMap;
				item.showLocation = onShowLocation;
				item.parentStruct = data;
				item.show({desc:obj.value,type:obj.type,value:obj.value2});
				//				item.y = _detailContainer.height + 10;
				if(i > 0)
				{
					item.y = _itemList[i - 1].y + _itemList[i - 1].height + _stage.stageHeight * 0.02;;
				}
				_detailContainer.addChild(item);
				_itemList.push(item);
				if(obj.type == "commentList")
				{
					_commentItem = item;	
				}
			}
			
			
			if(dataList.length > 0)
			{
				_aroundGroup = new AroundGroup(_stage,_loader);
				_aroundGroup.openDetail(updateSelf);
				_aroundGroup.show(data.aroundList,dataList);
				_aroundGroup.y = _detailContainer.height + _stage.stageHeight * 0.02;
				_detailContainer.addChild(_aroundGroup);
			}
			draw();
			
			if(_container.height < _stage.stageHeight * (1 - ClientConstants.HOME_DOWN_PER - ClientConstants.HOME_UP_PER))  return;
			StaticSlide.beginSlide(_container,_stage,new Rectangle(0,_container.y,0,-_container.height + _stage.stageHeight * (1 - ClientConstants.HOME_UP_PER - ClientConstants.HOME_DOWN_PER) - 20),_container.y);
		}
		
		private function draw():void
		{
			_container.graphics.beginFill(0,0);
			_container.graphics.drawRect(0,0,_stage.stageWidth,_container.height);
			_container.graphics.endFill();
		}
		
		private function updateSelf(data:Object):void
		{
			clear();
			var preStr:String = "";
			var callBack:Function = function():void
			{
				show(data,_dataList);
			}
			if(_type == ClientConstants.DETAIL_HOUSE)
			{
				preStr = "hotel/";
			}else if(_type == ClientConstants.DETAIL_VIEWSPOT)
			{
				preStr = "viewSpot/";
			}else if(_type == ClientConstants.DETAIL_SHOP)
			{
				preStr = "scenicShop/";
			}else if(_type == ClientConstants.DETAIL_EAT)
			{
				preStr = "eat/";
			}
			var resList:Array = [];
			if(data.picList.length > 0)
			{
				for(var i:int=0;i<data.picList.length;i++)
				{
					var url:String = _uri.getViewSpotDetailPic(preStr,data.picList[i]);
					var resData:ResourceLoaderData = new ResourceLoaderData(url);
					data["picUrl"].push(url);
					resList.push(resData);
				}
				_loader.load(resList,new Function,callBack);
			}
			
		}
		
		private function layout():void
		{
			
			_btnsGroup.y = _picGroup.y + _picGroup.height;
			
			_nameTxt.y = _btnsGroup.y + _btnsGroup.height + ROW_DIS;
			
			_detailContainer.y = _nameTxt.y + _nameTxt.height - _stage.stageHeight * 0.02;
			_container.addChild(_nameTxt);
			_container.addChild(_picGroup);
			_container.addChild(_detailContainer);
		}
		
		private function centerLine(line:Bitmap):void
		{
			line.width = _stage.stageWidth * 0.9;
			line.x = (_stage.stageWidth - line.width) * 0.5;	
		}
		
		public function set openTextPanel(value:Function):void
		{
			_openTextPanel = value;
		}
		
		public function set openCommentPanel(value:Function):void
		{
			_openCommentPanel = value;
		}
		
		public function set openMap(value:Function):void
		{
			_openMap = value;
		}
		
		public function set openWeibo(value:Function):void
		{
			_onOpenWeibo = value;
		}
		
		public function set addPath(value:Function):void
		{
			_addPath = value;
		}
		
		public function set showLocation(func:Function):void
		{
			onShowLocation = func;
		}
		
		public function clear():void
		{
			StaticSlide.clear(_container);
			_picGroup.clear();
			_picGroup.parent.removeChild(_picGroup);
			var item:ViewSpotDetailItem;
			for(var i:int=0;i<_itemList.length;i++)
			{
				item = _itemList[i];
				item.clear();
				_detailContainer.removeChild(item);
			}
			_itemList = [];
			_detailContainer.removeChildren();
			_container.removeChildren();
			_aroundGroup = null;
		}
	}
}